package gov.pr.celepar.sentinela.admin.action.restricao;

import java.util.Collection;

import gov.pr.celepar.framework.util.Pagina;
import gov.pr.celepar.sentinela.admin.action.SentinelaAction;
import gov.pr.celepar.sentinela.admin.excecao.SentinelaServerException;
import gov.pr.celepar.sentinela.admin.facade.RestricaoIPFacade;
import gov.pr.celepar.sentinela.admin.form.restricao.RestricaoIPSistemaForm;
import gov.pr.celepar.sentinela.admin.pojo.RestricaoIP;
import gov.pr.celepar.sentinela.admin.pojo.RestricaoIPSistema;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class RestricaoIPSistemaAction extends SentinelaAction {

	public ActionForward iniciar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws SentinelaServerException, Exception {

		setActionForward(mapping.findForward("lista_restricoes_ip_sistema"));
		this.listarSistemasAutorizados(request);

		return getActionForward();
	}

	public ActionForward pesquisar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws SentinelaServerException, Exception {
		RestricaoIPSistemaForm ipForm = (RestricaoIPSistemaForm) form;
		setActionForward(mapping.findForward("lista_restricoes_ip_sistema"));
		this.listarSistemasAutorizados(request);

		Long codSistema = ipForm.getCodSistema() != null ? Long.decode(ipForm
				.getCodSistema()) : null;

		Collection<RestricaoIP> restricoes = RestricaoIPFacade
				.getRestricoesIPSistema(codSistema);

		if (restricoes != null && !restricoes.isEmpty()) {
			int size = restricoes.size();
			Pagina pagina = new Pagina(size, 1, size);
			pagina.setRegistros(restricoes);

			request.setAttribute("pagina", pagina);
		} else {
			throw new SentinelaServerException("Nenhuma restrição encontrada! ");
		}

		return getActionForward();
	}

	public ActionForward alterar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws SentinelaServerException, Exception {
		RestricaoIPSistemaForm ipForm = (RestricaoIPSistemaForm) form;
		setActionForward(mapping.findForward("edit_restricao_ip_sistema"));
		this.listarSistemasAutorizados(request);

		Long codSistema = ipForm.getCodSistema() != null ? Long.decode(ipForm
				.getCodSistema()) : null;
		Long codRestricao = ipForm.getCodRestricao() != null ? Long
				.decode(ipForm.getCodRestricao()) : null;
		if (codSistema != null && codRestricao != null) {
			RestricaoIPSistema restricaoIPSistema = new RestricaoIPSistema();
			restricaoIPSistema.setCodRestricao(codRestricao);
			restricaoIPSistema.setEnderecoIP(ipForm.getIPFormatado());
			restricaoIPSistema.setAtiva(new Boolean(ipForm.isAtiva()));
			restricaoIPSistema.setCodSistema(codSistema);

			RestricaoIPFacade.alterar(restricaoIPSistema);

			this.addMessageSucess("Restrição por IP alterada com sucesso!",
					request);
		}
		return getActionForward();
	}

	public ActionForward exibir(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws SentinelaServerException, Exception {
		RestricaoIPSistemaForm ipForm = (RestricaoIPSistemaForm) form;
		setActionForward(mapping.findForward("view_restricao_ip_sistema"));
		this.listarSistemasAutorizados(request);

		Long codSistema = ipForm.getCodSistema() != null ? Long.decode(ipForm
				.getCodSistema()) : null;
		Long codRestricao = ipForm.getCodRestricao() != null ? Long
				.decode(ipForm.getCodRestricao()) : null;

		if (codSistema != null && codRestricao != null) {
			RestricaoIPSistema restricaoSistema = RestricaoIPFacade
					.getRestricaoIPSistema(codRestricao, codSistema);
			if (restricaoSistema != null) {
				ipForm.setAtiva(restricaoSistema.getAtiva().booleanValue());
				ipForm.setEnderecoIP(restricaoSistema.getEnderecoIP());
				ipForm.setNomeSistema(restricaoSistema.getNomeSistema());
			}
		}

		return getActionForward();
	}

	public ActionForward editar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws SentinelaServerException, Exception {
		RestricaoIPSistemaForm ipForm = (RestricaoIPSistemaForm) form;
		setActionForward(mapping.findForward("edit_restricao_ip_sistema"));
		this.listarSistemasAutorizados(request);

		Long codSistema = ipForm.getCodSistema() != null ? Long.decode(ipForm
				.getCodSistema()) : null;
		Long codRestricao = ipForm.getCodRestricao() != null ? Long
				.decode(ipForm.getCodRestricao()) : null;

		if (codSistema != null && codRestricao != null) {
			RestricaoIPSistema restricaoIPSistema = RestricaoIPFacade
					.getRestricaoIPSistema(codRestricao, codSistema);
			if (restricaoIPSistema != null) {
				ipForm.setAtiva(restricaoIPSistema.getAtiva().booleanValue());
				ipForm.setEnderecoIP(restricaoIPSistema.getEnderecoIP());
			}
		}

		return getActionForward();
	}

	public ActionForward excluir(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws SentinelaServerException, Exception {
		RestricaoIPSistemaForm ipForm = (RestricaoIPSistemaForm) form;
		setActionForward(mapping.findForward("view_restricao_ip_sistema"));
		this.listarSistemasAutorizados(request);

		Long codSistema = ipForm.getCodSistema() != null ? Long.decode(ipForm
				.getCodSistema()) : null;
		Long codRestricao = ipForm.getCodRestricao() != null ? Long
				.decode(ipForm.getCodRestricao()) : null;
		if (codSistema != null && codRestricao != null) {
			RestricaoIPSistema restricaoIPSistema = new RestricaoIPSistema();
			restricaoIPSistema.setCodRestricao(codRestricao);
			restricaoIPSistema.setEnderecoIP(ipForm.getIPFormatado());
			restricaoIPSistema.setAtiva(new Boolean(ipForm.isAtiva()));
			restricaoIPSistema.setCodSistema(codSistema);

			RestricaoIPFacade.excluir(restricaoIPSistema);

			this.addMessageSucess("Restrição por IP removida com sucesso!",
					request);

			Collection<RestricaoIP> restricoes = RestricaoIPFacade
					.getRestricoesIPSistema(codSistema);
			setActionForward(mapping.findForward("lista_restricoes_ip_sistema"));
			if (restricoes != null && !restricoes.isEmpty()) {
				int size = restricoes.size();
				Pagina pagina = new Pagina(size, 1, size);
				pagina.setRegistros(restricoes);

				request.setAttribute("pagina", pagina);
			} else {
				throw new SentinelaServerException(
						"Nenhuma restrição encontrada! ");
			}

		}
		return getActionForward();
	}

	public ActionForward incluir(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws SentinelaServerException, Exception {
		RestricaoIPSistemaForm ipForm = (RestricaoIPSistemaForm) form;
		setActionForward(mapping.findForward("edit_restricao_ip_sistema"));
		this.listarSistemasAutorizados(request);

		Long codSistema = ipForm.getCodSistema() != null ? Long.decode(ipForm
				.getCodSistema()) : null;
		if (codSistema != null) {
			String enderecoIP = ipForm.getIPFormatado();
			RestricaoIPSistema restricaoIPSistema = new RestricaoIPSistema();
			restricaoIPSistema.setCodSistema(codSistema);
			restricaoIPSistema.setAtiva(true);
			restricaoIPSistema.setEnderecoIP(enderecoIP);

			RestricaoIPFacade.salvar(restricaoIPSistema);

			this.addMessageSucess(
					"Restrição por IP cadastrada com sucesso para o Sistema!",
					request);

			Collection<RestricaoIP> restricoes = RestricaoIPFacade
					.getRestricoesIPSistema(codSistema);
			int size = restricoes.size();
			Pagina pagina = new Pagina(size, 1, size);
			pagina.setRegistros(restricoes);

			request.setAttribute("pagina", pagina);
			setActionForward(mapping.findForward("lista_restricoes_ip_sistema"));
		}

		return getActionForward();
	}
}
